package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.xmall.bean.table.*;
import com.xmall.bean.vo.mallindex.*;
import com.xmall.mapper.*;
import com.xmall.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Connor
 * @ClassName: HomeServiceImpl
 * @PackageName: com.xmall.service.impl
 * @date 2020/12/08
 * @Version 1.0
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GrouponMapper grouponMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;

    @Autowired
    AdMapper adMapper;

    @Override
    public MallIndexVo queryIndexData() {
        // 获取上新商品
        PageHelper.startPage(1, 6);
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        ArrayList<NewGoodsVo> newGoodsList = new ArrayList<>();
        for (Goods goods : goodsList) {
            newGoodsList.add(new NewGoodsVo(goods.getId(), goods.getName(), goods.getBrief(), goods.getPicUrl(), goods.getIsNew(), goods.getIsHot(), goods.getCounterPrice(), goods.getRetailPrice()));
        }
        // 获取优惠券信息
        PageHelper.startPage(1, 4);
        CouponExample couponExample = new CouponExample();
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        ArrayList<CouponVo> couponList = new ArrayList<>();
        for (Coupon coupon : coupons) {
            couponList.add(new CouponVo(coupon.getId(), coupon.getName(), coupon.getDesc(),
                    coupon.getTag(), coupon.getDiscount(), coupon.getMin(), coupon.getDays(), coupon.getStartTime(), coupon.getEndTime()));
        }
        // 获取商品分类信息
        PageHelper.startPage(1, 10);
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andPidNotEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        ArrayList<ChannelVo> channelList = new ArrayList<>();
        for (Category category : categories) {
            channelList.add(new ChannelVo(category.getId(), category.getName(), category.getIconUrl()));
        }
        // 获取团购信息
        PageHelper.startPage(1, 4);
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        ArrayList<GrouponVo> grouponVos = new ArrayList<>();
        for (GrouponRules grouponRule : grouponRules) {
            Goods goods = goodsMapper.selectByPrimaryKey(grouponRule.getGoodsId());
            GrouponGoodsVo goodsVo = new GrouponGoodsVo(goods.getId(), goods.getName(), goods.getBrief(), goods.getPicUrl(), goods.getCounterPrice(), goods.getRetailPrice());
            grouponVos.add(new GrouponVo(goods.getRetailPrice().subtract(grouponRule.getDiscount()), goodsVo, grouponRule.getDiscountMember()));
        }
        // 获取广告信息
        PageHelper.startPage(1, 4);
        AdExample adExample = new AdExample();
        AdExample.Criteria adExampleCriteria = adExample.createCriteria();
        adExampleCriteria.andDeletedEqualTo(false);
        List<Ad> adList = adMapper.selectByExample(adExample);
        ArrayList<BannerVo> bannerVos = new ArrayList<>();
        for (Ad ad : adList) {
            bannerVos.add(new BannerVo(ad.getId(), ad.getName(), ad.getLink(), ad.getUrl(), ad.getPosition(),
            ad.getContent(), ad.getEnabled(), ad.getAddTime(), ad.getUpdateTime(), ad.getDeleted()));
        }
        // 获取品牌信息
        PageHelper.startPage(1, 4);
        BrandExample brandExample = new BrandExample();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        ArrayList<BrandVo> brandVos = new ArrayList<>();
        for (Brand brand : brands) {
            brandVos.add(new BrandVo(brand.getId(), brand.getName(), brand.getDesc(), brand.getPicUrl(), brand.getFloorPrice()));
        }
        // 获取热门商品信息
        PageHelper.startPage(1, 6);
        GoodsExample hotGoodsExample = new GoodsExample();
        GoodsExample.Criteria hotGoodsExampleCriteria = hotGoodsExample.createCriteria();
        hotGoodsExampleCriteria.andIsHotEqualTo(true);
        List<Goods> hotGoodsList = goodsMapper.selectByExample(hotGoodsExample);
        ArrayList<HotGoodsVo> hotGoodsVos = new ArrayList<>();
        for (Goods goods : hotGoodsList) {
            hotGoodsVos.add(new HotGoodsVo(goods.getId(), goods.getName(), goods.getBrief(), goods.getPicUrl(),
                    goods.getIsNew(), goods.getIsHot(), goods.getCounterPrice(), goods.getRetailPrice()));
        }
        // 获取主题信息
        PageHelper.startPage(1, 4);
        TopicExample topicExample = new TopicExample();
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        ArrayList<TopicVo> topicVos = new ArrayList<>();
        for (Topic topic : topics) {
            topicVos.add(new TopicVo(topic.getId(), topic.getTitle(), topic.getSubtitle(),
                    topic.getPrice(), topic.getReadCount(), topic.getPicUrl()));
        }
        //floorGoods  等级查询商品信息
        PageHelper.startPage(1, 4);
        List<FloorGoodsCategoryVo> floorGoodsCategoryVos = categoryMapper.selectIdByLevel("L1");
        ArrayList<FloorGoodsVo> floorGoodsVos = new ArrayList<>();
        for (FloorGoodsCategoryVo floorGoodsCategoryVo : floorGoodsCategoryVos) {
            PageHelper.startPage(1, 4);
            List<Integer> ids = categoryMapper.selectIdByPid(floorGoodsCategoryVo.getId());
            for (Integer id : ids) {
                PageHelper.startPage(1, 4);
                List<FloorGoodsInfoVo> floorGoodsInfoVos = goodsMapper.selectByCategoryId(id);
                floorGoodsVos.add(new FloorGoodsVo(floorGoodsCategoryVo.getName(), floorGoodsInfoVos, floorGoodsCategoryVo.getId()));
            }
        }
        MallIndexVo mallIndexVo = new MallIndexVo(newGoodsList, couponList, channelList, grouponVos,
                bannerVos, brandVos, hotGoodsVos, topicVos, floorGoodsVos);
        return mallIndexVo;
    }
}
